//
//  PopBottomView.m
//  lyx
//
//  Created by apple on 2017/10/25.
//  Copyright © 2017年 seeday. All rights reserved.
//

#import "WeekLoopView.h"
#import "WeekLoopCell.h"
#import "UIButton+ImageTitleSpacing.h"
#import "UIView+ZFFrame.h"
#import "Sizes.h"
#import "TYMBaseTableView.h"
#import "Colors.h"

@interface WeekLoopView ()

@property(nonatomic,weak) TYMButton *saveBtn;
@property(nonatomic,strong) UIButton  *seleAllBtn;

@property(nonatomic,copy) NSMutableArray *seleArray;
@property(nonatomic,copy) NSMutableArray *showSele;
@end

@implementation WeekLoopView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.bounds = [UIScreen mainScreen].bounds;
        self.alpha = 0.0;
        self.backgroundColor = [UIColor blackColor];
        
        self.view_bg = [[UIView alloc]initWithFrame:self.bounds];
        self.view_bg.backgroundColor = [UIColor whiteColor];
        self.view_bg.alpha = 0;
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.view_bg.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
        maskLayer.frame = self.view_bg.bounds;
        maskLayer.path = maskPath.CGPath;
        self.view_bg.layer.mask = maskLayer;
        
        self.tableView = [[TYMBaseTableView  alloc]initWithFrame:self.view_bg.bounds];
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view_bg addSubview:self.tableView];
        
        self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.cancelBtn setImage:[UIImage imageNamed:@"icon_equipment6"] forState:UIControlStateNormal];
        [self.cancelBtn setTitleColor:rgba(74, 74, 74) forState:UIControlStateNormal];
        [self.cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [self.cancelBtn setFrame:CGRectMake(15, 15, 24, 24)];
        [self.cancelBtn addTarget:self action:@selector(cancelView:) forControlEvents:UIControlEventTouchUpInside];
        [self.view_bg addSubview:self.cancelBtn];
        
        self.seleAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.seleAllBtn setImage:[UIImage imageNamed:@"icon_home13"] forState:UIControlStateNormal];
        [self.seleAllBtn addTarget:self action:@selector(seleAllBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.seleAllBtn setTitle:@"全选" forState:UIControlStateNormal];
        [self.seleAllBtn setTitleColor:rgba(74, 74, 74) forState:UIControlStateNormal];
        [self.seleAllBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [self.seleAllBtn setFrame:CGRectMake(ScreenWidth - 90, 15, 80, 25)];
        [self.seleAllBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:5];
        [self.view_bg addSubview:self.seleAllBtn];
        
        
        self.titleLab = [[UILabel alloc] init];
        [self.titleLab setText:@"周循环"];
        self.titleLab.font = Font_Regular(18);
        self.titleLab.textAlignment = NSTextAlignmentCenter;
        [self.titleLab setTextColor:rgba(74, 74, 74)];
        [self.view_bg addSubview:self.titleLab];
        self.titleLab.frame = CGRectMake(ScreenWidth/2 - 50, 15, 100, 25);
  
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = rgba(216, 219, 227);
        lineView.frame = CGRectMake(8, 45, ScreenWidth - 16, 1);
        [self.view_bg addSubview:lineView];
        
        TYMButton *saveBtn = [TYMButton btn_text:@"保存" color:rgba(53, 54, 70) font:Font_Regular(17)];
        [saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
        saveBtn.frame = CGRectMake(15, 403, ScreenWidth - 30, 44);
        saveBtn.layer.cornerRadius = 5;
        saveBtn.layer.masksToBounds = YES;
        saveBtn.layer.borderColor = rgba(151, 151, 151).CGColor;
        saveBtn.layer.borderWidth = 1;
        [self.view_bg addSubview:saveBtn];
        self.saveBtn = saveBtn;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBgAction:)];
        tap.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tap];
        
        _data = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
        
        CGFloat height_ = _data.count * 55;
        
        CGRect rect_bg = self.view_bg.frame;
        rect_bg.size.height = height_ + 50 + SafeBottomMargin + 74;
        rect_bg.origin.y = ScreenHeight - rect_bg.size.height;
        [self.view_bg setFrame:rect_bg];
        
        
        CGRect rect_tb = self.tableView.frame;
        rect_tb.size.height = height_;
        rect_tb.origin.y = 50;
        [self.tableView setFrame: rect_tb];
        [self.tableView reloadData];
        
        self.saveBtn.zf_y = self.tableView.zf_bottom+10;
    }
    return self;
}

-(void)cancelView:(id)sender{
    [self removeOperateView];
}

-(NSMutableArray*)seleArray{
    if (_seleArray == nil) {
        _seleArray = [[NSMutableArray alloc] init];
    }
    return _seleArray;
}

-(NSMutableArray*)showSele{
    if (_showSele == nil) {
        _showSele = [[NSMutableArray alloc] init];
    }
    return _showSele;
}

-(void)setSeleStr:(NSString*)weekStr{

    if ([weekStr isEqualToString:@"1111111"]) {
        self.seleAllBtn.selected = YES;
        [self.seleAllBtn setImage:[UIImage imageNamed:@"icon_home12"] forState:UIControlStateNormal];
    }else{
        self.seleAllBtn.selected = NO;
        [self.seleAllBtn setImage:[UIImage imageNamed:@"icon_home13"] forState:UIControlStateNormal];
    }
    
    for(NSInteger i=0;i<weekStr.length
         ; i++) {
        [self.seleArray addObject:[weekStr substringWithRange:NSMakeRange(weekStr.length - i - 1,1)]];
        
        if ([[weekStr substringWithRange:NSMakeRange(weekStr.length - i - 1,1)] integerValue]) {
            [self.showSele addObject:_data[i]];
        }
    }
}

//全选
-(void)seleAllBtnAction:(UIButton*)sender{
    
    [self.seleArray removeAllObjects];
    [self.showSele removeAllObjects];
    
    if (sender.selected) {
        [self setSeleStr:@"0000000"];
    }else{
        [self setSeleStr:@"1111111"];
    }
    [self.tableView reloadData];
}

//保存
-(void)saveAction:(UIButton*)sender{
    
    self.seleArray = [[self.seleArray reverseObjectEnumerator] allObjects];
    
    if (self.blockCallBackIndex) {
        self.blockCallBackIndex([[self.seleArray componentsJoinedByString:@","] stringByReplacingOccurrencesOfString:@"," withString:@""], [self.showSele componentsJoinedByString:@","]);
    }
    [self removeOperateView];
}

#pragma mark----tableView Delegate or dataSource------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _seleArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentify = @"cellIdentify";
    WeekLoopCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (cell == nil) {
        cell = [[WeekLoopCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify];
    }
    cell.lab_title.text = _data[indexPath.row];
    BOOL show = [_seleArray[indexPath.row] integerValue];
    [cell setImageIsShow:show];
    return cell;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *sel = _seleArray[indexPath.row];
    if ([sel integerValue]) {
        [_seleArray replaceObjectAtIndex:indexPath.row withObject:@"0"];
         [self.showSele removeObject:_data[indexPath.row]];
    }else{
        [_seleArray replaceObjectAtIndex:indexPath.row withObject:@"1"];
         [self.showSele addObject:_data[indexPath.row]];
    }
    
    if ([[self.seleArray componentsJoinedByString:@""] isEqualToString:@"1111111"]) {
        self.seleAllBtn.selected = YES;
        [self.seleAllBtn setImage:[UIImage imageNamed:@"icon_home12"] forState:UIControlStateNormal];
    }else{
        self.seleAllBtn.selected = NO;
        [self.seleAllBtn setImage:[UIImage imageNamed:@"icon_home13"] forState:UIControlStateNormal];
    }
    [self.tableView reloadData];
}

-(void)viewShow{
    [[[[UIApplication sharedApplication] windows] firstObject]  addSubview:self];
    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:self.view_bg];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.5;
        self.view_bg.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}


-(void)removeOperateView{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.0;
        self.view_bg.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.view_bg removeFromSuperview];
    }];
}


#pragma mark-----action-------
-(void)tapBgAction:(UITapGestureRecognizer *)tap{
    
    [self removeOperateView];
}

-(void)dealloc{
    
    NSLog(@"这个周试图 滚蛋了 卧槽");
}

@end
